package SGGStu.ReverseList;

/**
 * @Author: Dijah
 * @Date: 2022/8/16 13:38
 * @Desc:
 */
public class reverseListNode {

    public static ListNode reverse(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode newNode = reverse(head.next);
        head.next.next = head;
        head.next = null;
        return newNode;
    }

    public static ListNode reverse1(ListNode head1){
        if(head1 == null || head1.next == null){
            return  head1;
        }
        ListNode pre = null;
        ListNode next = null;
        while (head1 != null){
            next = head1.next;
            head1.next = pre;
            pre = head1;
            head1 = next;

        }
        return pre;
    }

    public static void main(String[] args) {
        ListNode listNode = new ListNode();
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
//        ListNode listNode3 = new ListNode(2, listNode2);
//        ListNode listNode4 = new ListNode(3, listNode3);
//        ListNode listNode5 = new ListNode(4, listNode4);
        listNode.next = listNode1;
        listNode1.next =  listNode2;
        listNode2.next =  listNode3;
        listNode3.next =  listNode4;
        listNode4.next =  listNode5;
        System.out.println(listNode);
        ListNode reverse = reverse(listNode);
        System.out.println(reverse);

    }
}
